﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;
using System.IO;


namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// 广州WMS销售订单取消校验
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WMSSalesOrderCheckController : ControllerBase
    {
        private readonly ILogger _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;
        public readonly string gz_con;


        public WMSSalesOrderCheckController(ILogger<WMSSalesOrderCheckRequestDto> logger, IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration, ILoggerFactory loggerFactory)
        {
            _WMSTokenCheckService = wmsTokenCheckService;
            con = _configuration.GetConnectionString("DDM_DbConnection_ZH");
            gz_con = _configuration.GetConnectionString("GZ_WMS_DbConnection");
            _logger = loggerFactory.CreateLogger<WMSSalesOrderCheckController>();

        }

        /// <summary>
        /// 广州WMS销售订单取消校验
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostOrderCheck([FromBody] WMSSalesOrderCheckRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            WMSCustomTraceChangeResponseDto re = new WMSCustomTraceChangeResponseDto();
            string order_no = strparm.order_no;


            if (order_no.Trim() == "")
            {
                strMsg = "order_no入库单号不能为空，请检查后再试！";
            }

         


            Dictionary<string, string> dic = new Dictionary<string, string>(); //查询公用dic
            try
            {
                if (strMsg == "")
                {
                    dic = new Dictionary<string, string>();
                    dic.Add("SOREFERENCE3", order_no);
                    string sqlstr = MethodHelp.GetTranSQL("TMS0159", dic, con);//获取变更托盘原货主
                    DataTable check_ccode = DB.OracleQuery(sqlstr.ToString(), gz_con);//Oracle驱动查询
                    if (check_ccode.Rows.Count > 0)
                    {
                        if (check_ccode.Rows[0]["ORDERSTATUS"].ToString()!="90")
                        {
                            strMsg = "单据号=<" + order_no + ">WMS系统未取消，CRM取消单据异常，请联系仓库取消后再进行操作！";
                        }
                    }
                    else
                    {
                        strMsg = "找不到单据号=<"+ order_no + ">相关信息，请确认后再试！";
                    }


                }               
            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }
            if (strMsg=="")
            {
                re.code = 0;
            }
            else
            {
                re.code = 500;
                re.message = strMsg;
            }

            _logger.LogInformation(JsonConvert.SerializeObject(re));
            return new JsonResult(re);
        }

      
    }
}
